<?php
namespace app\index\controller;

use app\common\model\Chat;
use app\common\model\Info;
use app\common\model\Qqjob;
use app\common\model\Qqs;
use app\common\model\Gg;
use app\common\model\Users;
use app\common\model\Weblist;
use app\common\model\Prizes;
use app\common\model\Prizelog;
use app\common\model\Qds;
use app\common\model\Jfdh;

class Mobile extends Common
{
	public function _empty()
    {
        exit($this->fetch('mobile/common/empty'));
    }
	
	function __construct()
    {
        parent:: __construct();
        //检测用户VIP是否过期
        if (strtotime($this->userInfo['viptime']) < time()) {
            Users::where('uid', '=', $this->userInfo['uid'])->update(['vipstart' => NULL, 'viptime' => NULL]);
        }
        $this->qqInfo['count'] = Qqs::getMyQqCount($this->userInfo['uid']);
        $this->assign('qqInfo', $this->qqInfo);
		
    }
	
    public function index()
    {
		echo $this->fetch();
    }
	
	public function home()
    {
		$select = Gg::where('web_id', '=', WEB_ID)->select();
             $gglist ='';
			foreach ($select as $row) {
				switch ($row['type']) {
					case 0:
						 $gglist .='<div class="in-list in-b in-bb bg-bai ggw">
						<dl class="in p3">
							<dt><i class="icon icon-hot f7 fen mr2"></i></dt>
							<dd>
								<p><font class="fen mr1">[公告]</font>' . $row['con'] . '</p>
							</dd>
						</dl>
					</div> ';
											break;
										case 1:
											 $gglist .='<div class="in-list in-b in-bb bg-bai ggw">
						<dl class="in p3" onclick="x.url(\'' . $row['url'] . '\')">
							<dt><i class="icon icon-hot f7 hong mr2"></i></dt>
							<dd>
								<p><font class="hong mr1">[推荐] </font>' . $row['con'] . '</p>
							</dd>
							<dt><i class="icon icon-pright hui f7"></i></dt>
						</dl>
					</div>';
						break;
				}
			}
		$this->assign([
			'webTitle'	=>	'用户首页',
			'gglist'	=>	$gglist
		]);
		echo $this->fetch('/mobile/home/index');

    }
	
	public function info()
    {
        $this->assign('webTitle', '个人资料');
        echo $this->fetch('/mobile/home/info');
    }
	
	public function qqlist($act)
    {
        switch ($act) {
			case 'add':
				if(!input('get.qid')){
					$this->assign('row', '');
					$this->assign('webTitle', '添加QQ');
				}else{
					$this->assign('row', Qqs::findByqid(input('get.qid')));
					$this->assign('webTitle', '更新QQ');
				}
                echo  $this->fetch('/mobile/qq/add');
                break;
			case 'saoma':
				if(!input('get.qid')){
					$this->assign('row', '');
					$this->assign('webTitle', '扫码添加QQ');
				}else{
					$this->assign('row', Qqs::findByqid(input('get.qid')));
					$this->assign('webTitle', '扫码更新QQ');
				}
                echo  $this->fetch('/mobile/qq/saoma');
                break;
            case 'set':
                $qq = input('get.qq');
                $qq_data = Qqs::where('qq', '=', $qq)
                    ->where('uid', '=', $this->userInfo['uid'])
                    ->find();
                $this->assign('row', $qq_data);
                $this->assign('webTitle', '功能设置');
                echo  $this->fetch('/mobile/qq/set');
                break;
			 case 'info':
                $this->assign('webTitle', '秒赞管理');
             echo $this->fetch('/mobile/home/qqlist');
                break;
        }
    }
	
	public function other($act)
    {
        switch ($act) {
			case 'link':
                $this->assign('webTitle', '快捷链接');
                echo $this->fetch('/mobile/home/link');
                break;
            case 'dlyz':
                $this->assign('webTitle', '秒赞代理认证');
                echo $this->fetch('/mobile/other/dlyz');
                break;
            case 'mzq':
                $this->assign('webTitle', 'QQ秒赞墙');
                echo $this->fetch('/mobile/other/mzq');
                break;
			case 'mzrz':
                $this->assign('webTitle', '秒赞认证');
                echo $this->fetch('/mobile/other/mzrz');
                break;
			case 'qdlist':
                $this->assign('webTitle', '签到记录');
                echo $this->fetch('/mobile/other/qdlist');
                break;
			case 'dhlist':
                $this->assign('webTitle', '兑换记录');
                echo $this->fetch('/mobile/other/dhlist');
                break;
			case 'cjlist':
                $this->assign('webTitle', '抽奖记录');
                echo $this->fetch('/mobile/other/cjlist');
                break;
			case 'qd':
				switch (config('sys_sqd_give_type')) {
					case 1:
						$shop = '' . config('sys_sqd_shop_num') . '天VIP会员';
						break;
					case 2:
						$shop = '' . config('sys_sqd_shop_num') . '个配额';
						break;
					case 3:
						$shop = '' . config('sys_sqd_shop_num') . '元余额';
						break;
					default:
						$shop = '站长还没有配置签到奖品';
				}
				if (config('sys_qd_user_give_jf') == '') {
					$user_jf = '0,0';
				} else {
					$user_jf = config('sys_qd_user_give_jf');
				}
				$data = [
					'user_jf' => explode(",", $user_jf),
					'give_shop' => $shop,
					'day' => Qds::findDayByUid($this->userInfo['uid'])
				];
				if(Qds::isTodayQds($this->userInfo['uid'])){
					$button = '<button class="btn btn-hui btn-md w">今 日 已 签 到</button>
                        </button>';
				}else{
					$button = '<button class="btn btn-qing btn-md w" onclick="x.mode(\''.url('mobile_html/other',['act'=>'qd']).'\');">点 此 签 到</button>
                        </button>';
				}
				$this->assign('button', $button);
				$this->assign('qd', $data);
				$this->assign('webTitle', '每日签到');
				echo $this->fetch('/mobile/other/qd');
			break;
			case 'jfdh':
				$this->assign('webTitle', '积分兑换');
				$this->assign('dlname', is_Daili(config('sys_jfdh_daili_num')));
				echo $this->fetch('/mobile/other/jfdh');
			break;
			case 'jfcj':
				$list = Prizes::order('addtime desc')
				->select();
				$jplist='';
				foreach ($list as $row) {
					$jplist .= '<span class="tag bg-qing">' . $row['name'] . '</span>
					';
				}
				$zjcount = Prizelog::where('uid' ,'=' ,$this->userInfo['uid'])->count();
				$this->assign('zjcount', $zjcount);
				$this->assign('jplist', $jplist);
				$this->assign('webTitle', '积分抽奖');
				echo $this->fetch('/mobile/other/jfcj');
			break;
        }
    }
	
	public function shop($act)
    {
        switch ($act) {
            case 'rmb':
                $this->assign('webTitle', '余额充值');
                echo $this->fetch('/mobile/shop/rmb');
                break;
            case 'vip':
                $this->assign('webTitle', '开通会员');
                echo $this->fetch('/mobile/shop/vip');
                break;
			case 'peie':
                $this->assign('webTitle', '购买配额');
                echo $this->fetch('/mobile/shop/peie');
                break;
			case 'daili':
                $this->assign('webTitle', '开通代理');
                echo $this->fetch('/mobile/shop/daili');
                break;
        }
    }
	
	public function chat()
    {
		$userInfo = Users::getLoginUser();
		$msglist ='';
		$id = is_numeric(input('post.id')) ? input('post.id') : '0';
                $data = Chat::where('id', '>', $id)
                    ->order('id')
                    ->select();
                if ($data) {
                    foreach ($data as $row) {
                        $user = Users::where('uid', '=', $row['uid'])->find();
                        $diff = timediff(strtotime($user['vipstart']), strtotime($user['viptime']));
						$color = 'hong';
						if($user && $diff < 1 && $user['dlpower'] == 0){
							$color = '';
							$pc_sf = '';
							$m_sf = '';
						}elseif($user && $diff < 30 && $user['dlpower'] == 0){
							$color = '';
							$pc_sf = '<span class="bg-lan f3 pl1 pr1 rx1 ml2">体验VIP</span>';
							$m_sf = '<span class="bg-lan pl1 pr1 rx1 mr1">体验VIP</span>';
						}elseif($user && $diff > 29 && $diff < 90 && $user['dlpower'] == 0) {
							$pc_sf = '<span class="bg-lan f3 pl1 pr1 rx1 ml2">包月VIP</span>';
							$m_sf = '<span class="bg-lan pl1 pr1 rx1 mr1">包月VIP</span>';
						}elseif($user && $diff > 89 && $diff < 180 && $user['dlpower'] == 0) {
							$pc_sf = '<span class="bg-lan f3 pl1 pr1 rx1 ml2">包季VIP</span>';
							$m_sf = '<span class="bg-lan pl1 pr1 rx1 mr1">包季VIP</span>';
						}elseif($user && $diff > 179 && $diff < 365 && $user['dlpower'] == 0) {
							$pc_sf = '<span class="bg-zi f3 pl1 pr1 rx1 ml2">包季VIP2</span>';
							$m_sf = '<span class="bg-zi pl1 pr1 rx1 mr1">包季VIP2</span>';
						}elseif($user && $diff > 364 && $user['dlpower'] == 0) {
							$pc_sf = '<span class="bg-huang f3 pl1 pr1 rx1 ml2">年费VIP</span>';
							$m_sf = '<span class="bg-huang pl1 pr1 rx1 mr1">年费VIP</span>';
						}elseif($user['dlpower'] == 1) {
							$pc_sf = '<span class="bg-lan f3 pl1 pr1 rx1 ml2">银牌代理</span>';
							$m_sf = '<span class="bg-lan pl1 pr1 rx1 mr1">银牌代理</span>';
						}elseif($user['dlpower'] == 2) {
							$pc_sf = '<span class="bg-huang f3 pl1 pr1 rx1 ml2">金牌代理</span>';
							$m_sf = '<span class="bg-huang pl1 pr1 rx1 mr1">金牌代理</span>';
						}elseif($user['dlpower'] == 3) {
							$pc_sf = '<span class="bg-huang f3 pl1 pr1 rx1 ml2">钻石代理</span>';
							$m_sf = '<span class="bg-huang pl1 pr1 rx1 mr1">钻石代理</span>';
						}elseif($user['dlpower'] == 1) {
							$color = '';
							$pc_sf = '';
							$m_sf = '';
						}
						if ($row['uid']!== $userInfo['uid']) {
							$msglist .='<dl class="pt2 pb3"><dt><p class="w10 t-c"><img class="w7 h7 b r5" src="https://q4.qlogo.cn/headimg_dl?dst_uin=' . $row['qq'] . '&spec=100" onclick="chat_mode_footer(\'' . $row['nickname'] . '\',\'' . $row['qq'] . '\')"></p></dt> <dd> <p class="f2">'.$m_sf.'<font class="'.$color.' bold" >' . $row['nickname'] . '</font></p> <p><span class="inline bg-bai dl2 p2 rx3 copy" data-clipboard-text="' . $row['content'] . '">' . $row['content'] . '</span></p> </dd> <dt><p class="w10"></p></dt> </dl>';
						}elseif ($row['uid']== $userInfo['uid']){
							$msglist .='<dl class="pt2 pb3"><dt><p class="w10"></p></dt><dd class="t-r"><p class="f2">'.$m_sf.'<font class="'.$color.' bold" >' . $row['nickname'] . '</font></p><p><span class="inline bg-lan dl2 p2 rx3 copy" data-clipboard-text="' . $row['content'] . '">' . $row['content'] . '</span></p></dd><dt><p class="w10 t-c"><img class="w7 h7 b r5" src="https://q4.qlogo.cn/headimg_dl?dst_uin=' . $row['qq'] . '&spec=100"></p></dt></dl>';
						} 
					}
				}
		$this->assign([
			'web_id'	=>	WEB_ID,
			'msglist'	=>	$msglist,
			'webTitle'	=>	'CQY聊天室'
		]);
		echo $this->fetch('/mobile/chat/index');

    }
	
	public function user($act)
    {
        switch ($act) {
			case 'pass':
                $this->assign('webTitle', '修改密码');
                echo $this->fetch('/mobile/user/pass');
                break;
				case 'nickname':
                $this->assign('webTitle', '修改昵称');
                echo $this->fetch('/mobile/user/nickname');
                break;
				case 'qq':
                $this->assign('webTitle', '更改QQ');
                echo $this->fetch('/mobile/user/qq');
                break;
			case 'rmb':
                $this->assign('webTitle', '账户余额');
                echo $this->fetch('/mobile/home/rmb');
                break;
			case 'score':
				$this->assign('webTitle', '账户积分');
				echo $this->fetch('/mobile/home/score');
				break;
			case 'peie':
                $this->assign('webTitle', '挂机配额');
                echo $this->fetch('/mobile/home/peie');
                break;
			case 'vip':
                $this->assign('webTitle', '秒赞VIP会员');
                echo $this->fetch('/mobile/home/vip');
                break;
			case 'daili':
                $this->assign('webTitle', '代理后台');
				if(is_daili($this->userInfo['dlpower'])=='普通用户'){
					echo '<script>
					layer.open({
						content: \'<h4 class="mt0 mb4">权限不足</h4><p>您不是代理，没有权限访问哦</p>\',
						btn: \'我 知 道 了\',
						shadeClose: false,
						yes: function(i){
							x.mclose(i);
							x.mclose(moid["info_daili"]);mode_shop_daili();
							
						},
						no: function(i){
							x.mclose(i);
							
							
						}
					});
					</script>';
				}else{
					echo $this->fetch('/mobile/home/daili');
				}
                break;
            case 'gnlist':
				$uin = input('qid');
				$data = gnlist();
				$qqinfo = model('Qqs')->where('qq', '=', $uin)->find();
				$html='';
				foreach($data as $k=>$v){
					if($data[$k]['vip']==0){
						$is_vip = '<font class="f3 lv">免费</font>';
					}elseif($data[$k]['vip']==1){
						$is_vip = '<font class="f3 hong">VIP</font>';
					}else{
						$is_vip = '';
					}
					if (!Qqjob::findByType($data[$k]['id'], $uin)) {
						$html .='<dl> <a class="in p2" href="javascript:;" onclick="x.ajax(\''.url('mobile_ajax/gnset',['act'=>'add']).'\',\'type='.$data[$k]['id'].'&qid='.$qqinfo['qid'].'&uin='.$uin.'&gnid='.$data[$k]['gnid'].'\',1);"> <dt><img class="w6 h6 mr2" src="/Static/img/is'.$data[$k]['id'].'.png"></dt> <dd> <p class="pr3">添加 '.$data[$k]['name'].' 功能<span class="ml4">['.$is_vip.']</span></p> </dd> <dt><span class="pr2 f3 lv '.$data[$k]['id'].'access"></span></dt> <dt><i class="icon icon-pright hui"></i></dt> </a> </dl>';
					}else{
						$html .='<dl> <a class="in p2" href="javascript:;" onclick="x.msg(\'此功能已经添加在列表中\');"> <dt><img class="w6 h6 mr2" src="/Static/img/is'.$data[$k]['id'].'.png"></dt> <dd> <p class="pr3">添加 '.$data[$k]['name'].' 功能<span class="ml4">['.$is_vip.']</span></p> </dd> <dt><span class="pr2 f3 lv '.$data[$k]['id'].'access">已添加</span></dt> <dt><i class="icon icon-pright hui"></i></dt> </a> </dl>';
					}
				}
				$this->assign('html', $html);
                echo $this->fetch('/mobile/qq/gnlist');
                break;
        }
    }
	
	protected
    function _initialize()
    {
        parent::_initialize();
        $this->checkLogin();
		if(!check_mobile()){
			header('Location: '.url('pc/index').'');
			exit;
		}
		
    }
	
	private
    function checkLogin()
    {
        $check_Login = Users::checkLogin();
        if ($check_Login != null) {
            $this->assign('alert', $check_Login['msg']);
            $this->assign('url', $check_Login['url']);
            exit($this->fetch('mobile/common/msg'));
        } else {
            $this->userInfo = Users::getLoginUser();
            $this->assign('userInfo', $this->userInfo);

        }
    }
}
